<template>
  <div class="login-bg">
    <div class="login-center-card">
      <div class="login-logo">
        <img src="https://img.icons8.com/color/96/000000/movie-projector.png" alt="logo" />
        <h1>电影推荐系统</h1>
      </div>
      <el-card class="login-card">
        <div class="login-header">
          <h2>管理员登录</h2>
          <p class="login-subtitle">请输入您的账号信息</p>
        </div>
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="rules"
          label-width="0"
          @keyup.enter="handleLogin"
          class="login-form"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              :prefix-icon="User"
              size="large"
              clearable
              autofocus
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              :prefix-icon="Lock"
              show-password
              size="large"
              clearable
            />
          </el-form-item>
          <div class="login-options">
            <el-checkbox v-model="rememberMe">记住我</el-checkbox>
            <a href="#" class="forgot-password">忘记密码？</a>
          </div>
          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              class="login-button"
              size="large"
              @click="handleLogin"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>
        <div class="login-footer">
          <p>默认账号：admin</p>
          <p>默认密码：admin123</p>
        </div>
      </el-card>
      <div class="login-copyright">© 2024 电影推荐系统</div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'

defineOptions({
  name: 'LoginView'
})

const loading = ref(false)
const loginFormRef = ref(null)
const rememberMe = ref(false)
const router = useRouter()

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        if (loginForm.username === 'admin' && loginForm.password === 'admin123') {
          localStorage.setItem('isLoggedIn', 'true')
          localStorage.setItem('username', loginForm.username)
          if (rememberMe.value) {
            localStorage.setItem('rememberedUsername', loginForm.username)
          }
          ElMessage.success('登录成功')
          window.dispatchEvent(new Event('login-success'))
          router.push('/movies')
        } else {
          ElMessage.error('用户名或密码错误')
        }
      } catch (error) {
        ElMessage.error('登录失败')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-bg {
  position: fixed;
  left: 0; right: 0; top: 0; bottom: 0;
  min-height: 100vh;
  min-width: 100vw;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 0;
  overflow: hidden;
}
.login-center-card {
  width: 100%;
  max-width: 420px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1;
}
.login-logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 18px;
}
.login-logo img {
  width: 64px;
  height: 64px;
  margin-bottom: 8px;
  filter: drop-shadow(0 2px 8px #1565c0cc);
}
.login-logo h1 {
  font-size: 2em;
  color: #1565c0;
  font-weight: 700;
  letter-spacing: 2px;
  text-shadow: 0 2px 12px #e3eafc;
}
.login-card {
  width: 100%;
  border-radius: 22px;
  box-shadow: 0 8px 32px 0 #1565c040;
  background: #fff;
  border: none;
  padding: 32px 32px 18px 32px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin-bottom: 18px;
}
.login-header {
  text-align: center;
  margin-bottom: 18px;
}
.login-header h2 {
  margin: 0;
  color: #1565c0;
  font-size: 2em;
  font-weight: 700;
  letter-spacing: 1px;
}
.login-subtitle {
  margin: 10px 0 0;
  color: #90caf9;
  font-size: 1em;
}
.login-form {
  margin-top: 10px;
}
.login-form :deep(.el-input__wrapper) {
  background: #f6f8fa;
  border-radius: 12px;
  border: 1.5px solid #e3eafc;
  box-shadow: none;
  transition: border 0.2s;
}
.login-form :deep(.el-input__wrapper:hover),
.login-form :deep(.el-input__wrapper.is-focus) {
  border: 1.5px solid #1565c0;
  box-shadow: 0 0 8px #90caf955;
}
.login-form :deep(.el-input__inner) {
  height: 48px;
  font-size: 16px;
  background: transparent;
  color: #222;
}
.login-button {
  width: 100%;
  height: 48px;
  font-size: 18px;
  border-radius: 12px;
  font-weight: 600;
  letter-spacing: 2px;
  margin-top: 8px;
  background: linear-gradient(90deg, #1565c0 0%, #42a5f5 100%);
  border: none;
  color: #fff;
  box-shadow: 0 2px 8px 0 #1565c040;
  transition: all 0.3s cubic-bezier(.25,.8,.25,1);
}
.login-button:hover {
  filter: brightness(1.08);
  box-shadow: 0 6px 18px #42a5f555;
}
.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  margin-top: 2px;
}
.forgot-password {
  color: #1565c0;
  text-decoration: none;
  font-size: 15px;
  transition: color 0.3s ease;
}
.forgot-password:hover {
  color: #42a5f5;
}
.login-footer {
  text-align: center;
  margin-top: 18px;
  color: #90caf9;
  font-size: 0.98em;
}
.login-footer p {
  margin: 5px 0;
}
.login-copyright {
  color: #1565c0;
  font-size: 0.95em;
  margin-top: 10px;
  text-align: center;
  opacity: 0.7;
}
@media (max-width: 600px) {
  .login-center-card {
    max-width: 98vw;
    padding: 0 8px;
  }
  .login-card {
    padding: 18px 6px 10px 6px;
  }
  .login-logo h1 {
    font-size: 1.3em;
  }
}
</style> 